package com.breaktian.assemble.utils;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.PowerManager;
import android.support.v4.content.ContextCompat;
import android.telephony.TelephonyManager;
import android.text.TextUtils;


/**
 * Created by zerup on 17/4/14.
 */

public class DeviceUtil {
    public static boolean isNetworkAvailable(Context context) {
        ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity == null) {
            return false;
        }
        final NetworkInfo networkInfo = connectivity.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.isConnectedOrConnecting()) {
            return true;
        }
        return false;
    }

    /**
     * 获取IMEI
     * 检查电话状态的权限
     * 解决当手机os版本是6.0及以上出现权限错误 导致app无法启动
     * @return
     */
    public static String getDeviceId(Context context) {
        String imei = "";
        if(ContextCompat.checkSelfPermission(context,
                Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED){
            TelephonyManager telephonyManager = (TelephonyManager) context
                    .getSystemService(Context.TELEPHONY_SERVICE);
            imei = telephonyManager.getDeviceId();
        }
        return imei;
    }
    public static String getOsVersion(){
        String str = android.os.Build.VERSION.RELEASE;
        if (TextUtils.isEmpty(str)){
            return "";
        }
        return str;
    }

    public static String getDeviceName(){
        return android.os.Build.MODEL;
    }



    public static void keepScreenOn(Context context,boolean on){
        PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Lock");
        if(on){
            //保持常亮
            wakeLock.acquire();
        }else{
            //释放
            wakeLock.release();
        }
    }


}
